package pl.polidea.imf.blackberry.service;

import microlog.net.sf.microlog.core.Logger;
import microlog.net.sf.microlog.core.LoggerFactory;

import org.json.me.JSONObject;

import pl.polidea.imf.common.java.impl.storage.FileJSONItemStorage;
import pl.polidea.imf.common.java.impl.storage.FileJSONItemStorage.FileJSONItemStorageException;
import pl.polidea.imf.common.java.json.JSONSerializable;
import pl.polidea.imf.common.java.model.IssueItem;
import pl.polidea.imf.common.java.model.LogItem;
import pl.polidea.imf.common.java.net.CommunicationManager;
import pl.polidea.imf.common.java.storage.JSONItemStorage;
import pl.polidea.imf.common.java.storage.JSONItemStorage.JSONItemStorageException;

public class IMFEngine {
	
	static Logger logger = LoggerFactory.getLogger(IMFEngine.class);
	
	private static final String BASE_LOG_NAME = "log";
	private static final String BASE_ISSUE_NAME = "issue";
	private static final String BASE_CONDITION_NAME = "condition";
	
	public static final String LOG_DIRECTORY = "file:///store/home/user/imf/logs/";
	public static final String ISSUES_DIRECTORY = "file:///store/home/user/imf/issues/";
	public static final String CONDITION_DIRECTORY = "file:///store/home/user/imf/conditions/";
	
	
	private NetworkWorkerThread networkWorkerThread;
	private JSONItemStorage logStorage;
	private JSONItemStorage issueStorage;
	private JSONItemStorage conditionStorage;
	private CommunicationManager communicationManager;
	

	public IMFEngine(CommunicationManager communicationManager) {
		try {
			logStorage = new FileJSONItemStorage(LOG_DIRECTORY, BASE_LOG_NAME);
		} catch (FileJSONItemStorageException e) {
			logger.warn("cannot initialize log storage!", e);
		}
		try {
			issueStorage = new FileJSONItemStorage(ISSUES_DIRECTORY, BASE_ISSUE_NAME);
		} catch (FileJSONItemStorageException e) {
			logger.warn("cannot initialize issue storage!", e);
		}
		try {
			conditionStorage = new FileJSONItemStorage(CONDITION_DIRECTORY, BASE_CONDITION_NAME);
		} catch (FileJSONItemStorageException e) {
			logger.warn("cannot initialize condition storage!", e);
		}
		this.communicationManager = communicationManager;
		
		
		
	}
	
	public void startNetworkWorker(){
		if(networkWorkerThread!=null && networkWorkerThread.isAlive()){
			//ignore
		}
		else{
			networkWorkerThread = new NetworkWorkerThread(communicationManager,logStorage,issueStorage,conditionStorage, IMFService.getInstance().getSessionKey());
			networkWorkerThread.start();
		}
	}
	
	public void cleanStorage(){
		networkWorkerThread.cleanCache();
	}
	
	public void stopNetworkWorker(){
		if(networkWorkerThread!=null){
			networkWorkerThread.stopWorker();
		}
	}
		
	public void collectLog(LogItem logItem) {
		if(logStorage!=null){
			try {
				logStorage.collectItem(logItem);
			} catch (JSONItemStorageException e) {
				logger.warn("cannot collect log", e);
			}
		}
		else{
			logger.warn("cannot collect log: logStorage is null");
		}
	}
	
	public void collectIssue(IssueItem issueItem) {
		if(issueStorage!=null){
			try {
				issueStorage.collectItem(issueItem);
			} catch (JSONItemStorageException e) {
				logger.warn("cannot collect issue", e);
			}
		}
		else{
			logger.warn("cannot collect log: issueStorage is null");
		}
	}
	
	public void collectCondition(JSONObject condition){
		if(conditionStorage!=null){
			try {
				conditionStorage.collectItem(new JSONWrapper(condition));
			} catch (JSONItemStorageException e) {
				logger.warn("cannot collect issue", e);
			}
		}
	}
	
	private class JSONWrapper implements JSONSerializable{
		JSONObject encapsulated;
		
		public JSONWrapper(JSONObject json) {
			encapsulated = json;
		}

		public void fromJSON(JSONObject jsonObject) throws JSONSerializationException {
			encapsulated = jsonObject;
			
		}

		public JSONObject toJSON() throws JSONSerializationException {
			return encapsulated;
		}
		
		
	}
	
	

	
}
